{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1f7e7fa1-17c5-45a7-8cac-4f5081845c65",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "简化后的cosTheta表达式:\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sin{\\left(\\theta \\right)} \\sin{\\left(\\theta_{0} \\right)} \\cos{\\left(\\varphi - \\varphi_{0} \\right)} + \\cos{\\left(\\theta \\right)} \\cos{\\left(\\theta_{0} \\right)}$"
      ],
      "text/plain": [
       "sin(theta)*sin(theta_0)*cos(varphi - varphi_0) + cos(theta)*cos(theta_0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sympy import symbols, sin, cos, sqrt, simplify\n",
    "\n",
    "# 定义符号变量\n",
    "r, r_0, theta, phi, theta_0, phi_0 = symbols('r r_0 theta varphi theta_0 varphi_0')\n",
    "\n",
    "# 定义坐标变换\n",
    "x = r * sin(theta) * cos(phi)\n",
    "y = r * sin(theta) * sin(phi)\n",
    "z = r * cos(theta)\n",
    "\n",
    "# 定义带0的坐标变换\n",
    "x_0 = r_0 * sin(theta_0) * cos(phi_0)\n",
    "y_0 = r_0 * sin(theta_0) * sin(phi_0)\n",
    "z_0 = r_0 * cos(theta_0)\n",
    "\n",
    "# 计算距离L\n",
    "L = sqrt((x - x_0)**2 + (y - y_0)**2 + (z - z_0)**2)\n",
    "\n",
    "# 计算xxx表达式\n",
    "cosTheta = (r**2 + r_0**2 - L**2) / (2 * r * r_0)\n",
    "\n",
    "# 简化表达式\n",
    "cosTheta_simplified = simplify(cosTheta)\n",
    "\n",
    "# 显示结果\n",
    "print(\"\\n简化后的cosTheta表达式:\")\n",
    "display(cosTheta_simplified)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "566d01ad-c734-4d19-90b0-506ba7811ab0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
